<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * index.ctrl
 * 全部商品列表控制器
 */
defined('IN_IA') or exit('Access Denied');

$ops = array('admin_cabinet','teacher_door','student_door');
$rolearray = array('admin','teacher','student');
global $_W;
//先判断链接是否完整
if($_GPC['info_id']==''){
    $prompt = '用于开启柜子的链接不完整！';
    include wl_template('tips/prompt');
    exit;    
}
$info_id = $_GPC['info_id'];
$id_to_name = $this->get_id_to_name();
$device_name = $id_to_name[$info_id];
//进入if前，先判断role
$openid = $_W['openid'];
$current_user = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id','name','role')); 
if($current_user==null){
    $prompt = '您还没有与柜子或柜门进行绑定，无法使用开柜功能！';
    include wl_template('tips/prompt');
    exit;     
}else{
    if(!in_array($current_user['role'],$rolearray)){
        $prompt = '您的角色无法使用开柜功能！';
        include wl_template('tips/prompt');
        exit;         
    }else{
        if($current_user['role']==$rolearray[0]) $op=$ops[0];
        if($current_user['role']==$rolearray[1]) $op=$ops[1];
        if($current_user['role']==$rolearray[2]) $op=$ops[2];
    }
    
}


if($op =='admin_cabinet'){//已经判断过role
    //得到该管理员管理的柜子id
    $current_cabinet = array();
    $get = pdo_get('homework_cabinet_admin', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id), array('userid','device_info_id','device_name','door_num'));
    $temp = explode(',',$get['userid']);
    if(in_array($current_user['id'] , $temp)){
        for($i=1;$i<=intval($get['door_num']);$i++){
            $current_cabinet[] = array(
                'info_id' => $get['device_info_id'],          
                'door' => $i
            );
        }
    }
    
    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前扫描的柜子您还没有绑定！';
        include wl_template('tips/prompt');
        exit;         
    }
    foreach($current_cabinet as $key=>$value){
        $teacher_id = pdo_get('homework_door_teacher', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door'=>$value['door']), array('userid'));
        //这个id肯定只有一个id，因为是教师表
        if($teacher_id!=null & $teacher_id['userid']!=''){
            $teacher_name = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'id'=>$teacher_id['userid']), array('name')); 
            $current_cabinet[$key]['name'] = $teacher_name['name'];
        }
    }
    // print_r($current_cabinet);
    
	include wl_template('opendoor/admin_cabinet');
    exit;         
}
if($op =='teacher_door'){//已经判断过role
    //查看该教师是否有管理这个柜子的柜门
    $current_cabinet = array();
    $get = pdo_getall('homework_door_teacher', array('uniacid'=>$_W['uniacid'],'userid'=>$current_user['id'],'device_info_id'=>$info_id), array('device_info_id','door'));
    foreach($get as $key=>$value){
        $current_cabinet[] = array(
            'info_id' => $info_id,
            'door' => $value['door']
        );
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前扫描的柜子您还没有绑定！';
        include wl_template('tips/prompt');
        exit;
    }
    //根据柜门找到绑定的学生
    foreach($current_cabinet as $key=>$value){
        $student_id = pdo_get('homework_door_student', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door'=>$value['door']), array('userid'));
        $temp_arr = explode(',' , $student_id['userid']);
        $name_s = array();
        foreach($temp_arr as $k=>$val){
            $temp_name = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'id'=>$val), array('name'));
            $name_s[] = $temp_name['name'];
        }
        $name_string = implode(',',$name_s);
        $current_cabinet[$key]['name'] = $name_string;
    }
    
    // print_r($current_cabinet); 
	include wl_template('opendoor/teacher_door');
    exit;
}
if($op =='student_door'){//已经判断过role
    //查看该学生是否有管理这个柜子的柜门
    $current_cabinet = array();
    $get = pdo_getall('homework_door_student', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id), array('userid','device_info_id','door'));
    foreach($get as $key=>$value){
        $temp = explode(',',$value['userid']);
        if(in_array($current_user['id'] , $temp)){
            $current_cabinet[] = array(
                'info_id' => $info_id,          
                'door' => $value['door']
            );            
        }
    }
    //检查该数组，如果为空，则提示信息
    if(count($current_cabinet)==0){
        $prompt = '当前扫描的柜子您还没有绑定！';
        include wl_template('tips/prompt');
        exit;
    }
    //根据柜门找到绑定的老师
    foreach($current_cabinet as $key=>$value){
        $teacher_id = pdo_get('homework_door_teacher', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$info_id,'door'=>$value['door']), array('userid'));
        $teacher_name = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'id'=>$teacher_id['userid']), array('name')); 
        $current_cabinet[$key]['name'] = $teacher_name['name'];
    }   
    
    // print_r($current_cabinet);
	include wl_template('opendoor/student_door');
    exit;    
}

